package com.qianfeng.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.qianfeng.commons.DBManager;
import com.qianfeng.commons.DBUtils;
import com.qianfeng.dao.IGoodsTypeDao;
import com.qianfeng.entity.GoodType;

public class GoodsTypeDaoImpl extends DBUtils<GoodType> implements IGoodsTypeDao {

	@Override
	public List<GoodType> queryAllGoodsType() {
		String sql = "SELECT * from t_goods_type";
		return super.getList(sql, GoodType.class);
	}

	@Override
	public int queryCount() {
		String sql="SELECT count(*) from t_goods_type";
		int count=0;
		Connection connection = DBManager.getConnection();
		try {
			Statement st = connection.createStatement();
			
			ResultSet rs = st.executeQuery(sql);
			while(rs.next())
			count =rs.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return count;
	}

	@Override
	public List<GoodType> queryfenyeList(int rowIndex, int pageSize) {
		List<GoodType> list=new ArrayList<GoodType>();
		String sql="SELECT * from t_goods_type limit ?,?";
		Connection connection = DBManager.getConnection();
		try {
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setObject(1, rowIndex);
			ps.setObject(2, pageSize);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				GoodType gt = new GoodType();
				gt.setId(rs.getInt(1));
				gt.setGtype_name(rs.getString(2));
				gt.setGtype_parentid(rs.getInt(3));
				gt.setGtype_pic(rs.getString(4));
				list.add(gt);
				
			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	
		
	}

	@Override
	public GoodType getGoodTypeById(int typeid) {
		String sql = "select * from t_goods_type where id = ?";
		return getSingleInstance(sql, GoodType.class, typeid);
	}

	@Override
	public int deleteById(int typeid) {
		String sql = "delete from t_goods_type where id = ?";
		return commonOper(sql, typeid);
	}

	@Override
	public int updateGoodTypeInfo(GoodType goodType) {
		String sql = "update t_goods_type set gtype_name = ?,gtype_parentid = ? where id = ?";
		return commonOper(sql, goodType.getGtype_name(),goodType.getGtype_parentid(),goodType.getId());
	}
	

}
